package com.huike.web.controller.clues;


import com.huike.clues.domain.TbClue;
import com.huike.clues.domain.TbClueTrackRecord;
import com.huike.clues.domain.vo.ClueTrackRecordVo;
import com.huike.clues.mapper.TbClueMapper;
import com.huike.clues.mapper.TbClueTrackRecordMapper;
import com.huike.clues.service.ITbClueTrackRecordService;
import com.huike.common.annotation.Log;
import com.huike.common.core.controller.BaseController;
import com.huike.common.core.domain.AjaxResult;
import com.huike.common.core.page.TableDataInfo;
import com.huike.common.enums.BusinessType;
import com.huike.common.utils.DateUtils;
import com.huike.common.utils.SecurityUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;


/**
 * 线索跟进记录Controller
 * @date 2021-04-22
 */
@RestController
@RequestMapping("/clues/record")
public class TbClueTrackRecordController extends BaseController {

    @Autowired
    private ITbClueTrackRecordService iTbClueTrackRecordService;

    /**
     * 查询线索跟进记录列表
     */
    @PreAuthorize("@ss.hasPermi('clues:record:list')")
    @GetMapping("/list")
    public TableDataInfo list(@RequestParam("clueId")Long clueId) {
        TableDataInfo tableDataInfo = new TableDataInfo();
        List<ClueTrackRecordVo> list =  iTbClueTrackRecordService.selectClueTrackRecordById(clueId);
        tableDataInfo.setRows(list);
        tableDataInfo.setMsg("查询成功！");
        tableDataInfo.setCode(200);
        tableDataInfo.setTotal(list.size());
        return tableDataInfo;
    }


    /**
     * 新增线索跟进记录
     */
    @PreAuthorize("@ss.hasPermi('clues:record:add')")
    @Log(title = "线索跟进记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClueTrackRecordVo tbClueTrackRecord) {
        //创建线索跟进记录的对象
        TbClueTrackRecord trackRecord = new TbClueTrackRecord();
        //将前端传过来的对象拷贝到线索跟进记录对象中
        BeanUtils.copyProperties(tbClueTrackRecord,trackRecord);
        //给创建时间和创建人赋值
        trackRecord.setCreateTime(DateUtils.getNowDate());
        trackRecord.setCreateBy(SecurityUtils.getUsername());
        //创建一个线索对象，同时更新线索表里的记录
        TbClue tbClue = new TbClue();
        //拷贝对象到线索信息
        BeanUtils.copyProperties(tbClueTrackRecord,tbClue);

        //修改线索表的线索状态
        tbClue.setStatus(TbClue.StatusType.FOLLOWING.getValue());
        tbClue.setId(tbClueTrackRecord.getClueId());

        return AjaxResult.success(iTbClueTrackRecordService.insertClueTracke(trackRecord,tbClue));
    }
}
